package com.dialog.suotalib.ring;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

import com.dialog.suotalib.interfaces.callbacks.ISuotaScanner;
import com.dialog.suotalib.scan.SuotaScanner;
import com.dialog.suotalib.suota.GattOperation;
import com.dialog.suotalib.suota.SuotaManager;
import com.dialog.suotalib.suota.SuotaManagerCallback;
import com.dialog.suotalib.utils.RuntimePermissionChecker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class RingManagement {
    private Lifecycle lifecycle;
    private AppCompatActivity activity;
    private Bundle saveInstanceState;
    private SuotaScanner suotaScanner;
    private SuotaManager suotaManager;
    public byte[] SPACE = {(byte)0x5a, (byte) 0xa5, (byte) 0x83, (byte) 0xAA,(byte)0x01};
    public byte[] DESKTOP = {(byte)0x5a, (byte) 0xa5, (byte) 0x83, (byte) 0xAA,(byte)0x02};
    public static RingManagement ringManagement;

    public static RingManagement getRingManagement(Lifecycle lifecycle, AppCompatActivity activity, Bundle saveInstanceState) {
        if (ringManagement == null) {
            if (ringManagement == null)
                ringManagement = new RingManagement( lifecycle, activity,  saveInstanceState);
        }
        return ringManagement;
    }
    public RingManagement(Lifecycle lifecycle, AppCompatActivity activity, Bundle saveInstanceState) {
        this.lifecycle = lifecycle;
        this.activity = activity;
        this.saveInstanceState = saveInstanceState;
        suotaScanner = new SuotaScanner.Builder()
                .setLifecycle(lifecycle)
                .setActivity(activity)
                .setSaveInstanceState(saveInstanceState)
//                .setPermissionChecker(new RuntimePermissionChecker(activity))
                .build(); //蓝牙进行校验
        copyFileFromAssets("TEST.txt", activity.getFilesDir().getAbsolutePath() + "/save.txt");
        copyFileFromAssets("save.pt", activity.getFilesDir().getAbsolutePath() + "/save.pt");
    }

    public boolean isScanning() {
        return suotaScanner.isScanning();
    }
    public void stopScan() {
        suotaScanner.stopScan();
    }
    public void scan(@NonNull final ISuotaScanner scanCallback){
        suotaScanner.scan(scanCallback);
    }

    //链接蓝牙
    public  void connect(Lifecycle lifecycle, Context context, BluetoothDevice device, SuotaManagerCallback suotaManagerCallback){
        suotaManager = new SuotaManager(lifecycle, context, device, suotaManagerCallback);
        suotaManager.connect();
    }
    //断开链接
    public void disconnect(){
        suotaManager.disconnect();
    }
    public SuotaManager.ManagerState getState() {
        if (suotaManager!=null){
            return suotaManager.getState();
        }else {
          return   SuotaManager.ManagerState.DISCONNECTED;
        }

    }

    /**
     * 设置戒指模式
     *
     * @param data
     */
    public void setMode(byte[] data){
        suotaManager.enqueueGattOperation(new GattOperation(suotaManager.getTextRXCharacteristic(),data));
    }
    public void copyFileFromAssets(String fileName, String outputPath) {
        AssetManager assetManager = activity.getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(fileName);
            out = new FileOutputStream(outputPath);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
